package cn.lili.modules.member.entity.dto;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.crypto.digest.MD5;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.UuidUtils;
import cn.lili.modules.connect.entity.dto.ConnectAuthUser;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;

/**
 * UserInfoDTO
 *
 * @author Chopper
 * @version v1.0
 * 2022-08-24 14:13
 */
@SuperBuilder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserInfoDTO {

    /**
     * 用户信息传递参数
     * <p>
     * ID
     * 用户名
     * 手机号
     * 密码
     * 昵称
     * 性别 1为男 0为女 -1 未知
     * 头像
     * 场景
     */
    private String id;

    private String username;

    @Size(min = 11, max = 11, message = "手机号长度必须为11位")
    private String mobile;

    @Size(max = 50, message = "密码长度不能超过50位")
    private String password;

    @Size(min = 4, max = 20, message = "昵称长度必须在4-20之间")
    private String nickName;

    @Min(value = -1, message = "性别只能为-1,0,1")
    @Max(value = 1, message = "性别只能为-1,0,1")
    private Integer sex;

    @Size(max = 255, message = "头像图片地址长度不能超过255位")
    private String face = "https://feiyu-lshop-test.oss-cn-beijing.aliyuncs.com/default.png";


    @ApiModelProperty(value = "场景")
    private SceneEnums scene;
    @ApiModelProperty(value = "超级管理员")
    private Boolean isSuper = false;

    /**
     * 可以简介理解为场景下的id，与scene同步联用
     */
    @ApiModelProperty(value = "该场景下的id")
    private String extendId;
    /**
     * 可以简介理解为场景下的id，与scene同步联用
     */
    @ApiModelProperty(value = "该场景下的名称")
    private String extendName;

    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "生日")
    private Date birthday;

    @ApiModelProperty(value = "是否是店主")
    private Boolean shopkeeper;

    @Size(max = 255, message = "部门编号长度不能超过255位")
    @ApiModelProperty(value = "所属部门id")
    private String departmentId;

    @Size(max = 3, message = "最多赋予3个角色")
    @ApiModelProperty(value = "角色id集合")
    private List<String> roleIds;

    @ApiModelProperty(value = "城市分站Id")
    private String urbanSubstationId;


    @ApiModelProperty(value = "是否允许从1688采集商品")
    private Boolean grabAlibabaOpen=false;

    @ApiModelProperty("上级账号-添加上级时使用")
    private String parentUsername;

    public Date getBirthday() {
        if (birthday != null && birthday.after(new Date())) {
            throw new ServiceException(ResultCode.PARAMS_ERROR, "生日不能大于当前日期");
        }
        return birthday;
    }
    public String getRoleIdsStr() {
        if (roleIds != null && !roleIds.isEmpty()) {
            return String.join(",", roleIds);
        }
        return "";
    }

    public String getNickName() {
        if (CharSequenceUtil.isEmpty(nickName)) {
            return "临时昵称";
        }
        return nickName;
    }

    public UserInfoDTO(ConnectAuthUser connectAuthUser) {
        this.nickName = CharSequenceUtil.isEmpty(connectAuthUser.getNickname()) ? "临时昵称" : connectAuthUser.getNickname();
        this.sex = connectAuthUser.getGender() != null ? Convert.toInt(connectAuthUser.getGender().getCode()) : 0;
        this.face = CharSequenceUtil.isEmpty(connectAuthUser.getAvatar()) ?
                "https://i.loli.net/2020/11/19/LyN6JF7zZRskdIe.png" : connectAuthUser.getAvatar();
        this.username = UuidUtils.getUUID();
        this.password = MD5.create().digestHex(username.substring(this.username.length() - 6));
    }

}
